package com.bw.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson2.JSON;
import com.bw.domain.*;
import com.bw.service.OrderService;
import com.bw.utils.AesUtil;
import com.bw.utils.R;
import com.bw.utils.esypoi.ExportsUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: Wrj
 * @Date: 2025/3/9 13:33
 * @Description:
 **/
@Controller
@RequestMapping("order")
public class OrderController {
    @Autowired
    OrderService orderService;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    RabbitTemplate rabbitTemplate;


    @ResponseBody
    @RequestMapping("outreport")
    public R outreport(HttpServletResponse response){
        List<OrderReport> orderReportList=orderService.report(null,null,null);
        String failName= DateUtil.today()+ RandomUtil.randomNumbers(4);
        ExportsUtil.exports(response,OrderReport.class,orderReportList,"工单数据","工单列表",failName);

        return R.OK();
    }

    @ResponseBody
    @RequestMapping("listgroup")
    public R listgroup(String type){
        List<Dict> dictList=orderService.listgroup(type);
        return R.OK(dictList);
    }


    @RequestMapping("toReport")
    public String toReport(){
        return "order/report";
    }


    @ResponseBody
    @RequestMapping("report")
    public  R report(String startTime,String endTime,String groupId){
        long t1 = System.currentTimeMillis();
        List<OrderReport> orderReportList=orderService.report(startTime,endTime,groupId);

        long t2 = System.currentTimeMillis();
        long t=t2-t1;
        int count=orderReportList.size();
        HashMap<String, Object> map = new HashMap<>();
        map.put("count",count);
        map.put("time",t);
        map.put("orderReportList",orderReportList);
        return R.OK(map);

    }

    @ResponseBody
    @RequestMapping("add")
    public R add(Order order) throws Exception {
        order.setStatusType("0");
        String bz = AesUtil.encrypt(order.getBz(), "1234567890123456");
        order.setBz(bz);
        int i =orderService.add(order);
        return i>0?R.OK():R.ERROR(404,"添加失败");
    }

    @ResponseBody
    @RequestMapping("listorderType")
    public R listorderType(){

        List<Dict> listorderType = (List<Dict>) redisTemplate.opsForValue().get("listorderType");

        if (listorderType==null){
            listorderType=orderService.listorderType();
            redisTemplate.opsForValue().set("listorderType",listorderType);

        }


        return R.OK(listorderType);
    }


    @ResponseBody
    @RequestMapping("listcommunity")
    public R listcommunity(){
        List<Community> communityList=orderService.listcommunity();
        return R.OK(communityList);
    }


    @ResponseBody
    @RequestMapping("ok")
    public R ok(Order order){
        rabbitTemplate.convertAndSend("DIRECT_EX","roKey",order.getId()+"-"+order.getSubmitterPhone());

        return R.OK();
    }

    @RequestMapping("toadd")
    public String toadd(){
        return "order/add";
    }

    @ResponseBody
    @RequestMapping("pj")
    public R pj(Pj pj, HttpSession session){
        User user = (User) session.getAttribute("user");
        pj.setUsername(user.getUsername());
        int i =orderService.instrPj(pj);
        return i>0?R.OK():R.ERROR(404,"评价失败");
    }


    @RequestMapping("todetial")
    public String todetial(Integer id,Model model){
        Order order=orderService.listOrderById(id);
        model.addAttribute("order",order);

        List<OrderDetail> detailList=orderService.listORderDetail(id);

        model.addAttribute("detail",detailList);


        List<Pj> pjList=orderService.listPj(id);
        model.addAttribute("pj",pjList);
        return "order/detial";
    }

    @ResponseBody
    @RequestMapping("fp")
    public R fp(Order order, HttpSession session,String select){
        User user = (User) session.getAttribute("user");

        order.setUserId(user.getUserId());
        Boolean b =orderService.fp(order,select);
        return b?R.OK():R.ERROR(404,"分配失败");
    }

    @RequestMapping("tolist")
    public String tolist(Model model){

        List<Community> communityList=orderService.listCommunity();
        model.addAttribute("listCommunity", JSON.toJSON(communityList));

        List<Worker> workerList=orderService.listWorker();
        model.addAttribute("workerList",JSON.toJSON(workerList));

        return "order/list";
    }

    @ResponseBody
    @RequestMapping("list")
    public R list(Order order,Integer page,Integer limit){

        PageHelper.startPage(page,limit);
        List<Order> orderList=orderService.list(order);
        PageInfo<Order> pageInfo = new PageInfo<>(orderList);

        return R.OK(pageInfo);
    }
}
